package com.imooc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imooc.entity.Member;
import com.imooc.entity.MemberReadState;
import com.imooc.exception.BusinessException;
import com.imooc.exception.BusinessExceptionCode;
import com.imooc.mapper.MemberMapper;
import com.imooc.mapper.MemberReadStateMapper;
import com.imooc.service.MemberService;
import com.imooc.utils.MD5Utils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Random;

@Service("memberService")
@Transactional
public class MemberServiceImpl implements MemberService {

    @Resource
    private MemberMapper memberMapper;

    @Resource
    private MemberReadStateMapper memberReadStateMapper;

    @Override
    public Member selectById(Long memberId) {
        return memberMapper.selectById(memberId);
    }

    /**
     * 会员注册创建新会员
     * @param username 用户名
     * @param password 密码
     * @param nickname 昵称
     * @return 新会员对象
     */
    @Override
    public Member createMember(String username, String password, String nickname) {
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("username", username);
        Member member = memberMapper.selectOne(memberQueryWrapper);
        if (member != null) {
            throw new BusinessException(BusinessExceptionCode.MEMBER_IS_EXIST);
        }
        Integer salt = new Random().nextInt(1000) + 1000;
        String passwordMD5 = MD5Utils.md5Digest(password, salt);
        Member createMember = new Member();
        createMember.setUsername(username);
        createMember.setNickname(nickname);
        createMember.setPassword(passwordMD5);
        createMember.setSalt(salt);
        createMember.setCreateTime(new Date());
        memberMapper.insert(createMember);
        return createMember;
    }

    @Override
    public Member checkLogin(String username, String password) {
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("username", username);
        Member member = memberMapper.selectOne(memberQueryWrapper);
        if (member == null) {
            throw new BusinessException(BusinessExceptionCode.MEMBER_NOT_FOUND);
        }
        String memberPassword = member.getPassword();
        if (!memberPassword.equals(MD5Utils.md5Digest(password, member.getSalt()))) {
            throw new BusinessException(BusinessExceptionCode.MEMBER_PASSWORD_NOT_EQUALS);
        }
        return member;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public MemberReadState selectMemberReadState(Long memberId, Long bookId) {
        QueryWrapper<MemberReadState> memberReadStateQueryWrapper = new QueryWrapper<>();
        memberReadStateQueryWrapper.eq("member_id", memberId);
        memberReadStateQueryWrapper.eq("book_id", bookId);
        return memberReadStateMapper.selectOne(memberReadStateQueryWrapper);
    }

    /**
     * 更新阅读状态
     * @param memberId 会员编号
     * @param bookId 图书编号
     * @param state 阅读状态
     * @return 阅读状态对象
     */
    @Override
    public MemberReadState updateMemberReadState(Long memberId, Long bookId, Integer state) {
        MemberReadState memberReadState = selectMemberReadState(memberId, bookId);
        if (memberReadState != null) {
            memberReadState.setReadState(state);
            memberReadStateMapper.updateById(memberReadState);
        } else {
            memberReadState = new MemberReadState();
            memberReadState.setMemberId(memberId);
            memberReadState.setBookId(bookId);
            memberReadState.setReadState(state);
            memberReadState.setCreateTime(new Date());
            memberReadStateMapper.insert(memberReadState);
        }
        return memberReadState;
    }
}
